package com.itheima.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.Questions;
import com.itheima.domain.db.QuestionsOption;
import com.itheima.domain.vo.SoulTestQuestionVo;
import com.itheima.dubbo.mapper.QuestionsMapper;
import com.itheima.dubbo.mapper.QuestionsOptionMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author 阿鹏
 * @date 2021/3/1 20:55
 */
@Service
public class QuestionsApiImpl implements QuestionsApi {

    @Autowired
    private QuestionsMapper questionsMapper;
    @Autowired
    private QuestionsOptionMapper questionsOptionMapper;

    @Override
    public List<Questions> findListByCid(Long id) {
        QueryWrapper<Questions> wrapper = new QueryWrapper<>();
        wrapper.eq("cid", id);
        return questionsMapper.selectList(wrapper);
    }

    @Override
    public Questions findById(String questionId) {
        return questionsMapper.selectById(Long.valueOf(questionId));
    }

    @Override
    public void updataById(Questions questions) {
        questionsMapper.updateById(questions);
    }

    @Override
    public void insertQuestionAndOption(SoulTestQuestionVo data) {
        Questions questions = new Questions();
        questions.setCid(data.getCid());
        questions.setQuestion(data.getQuestion());
        questionsMapper.insert(questions);
        List<QuestionsOption> options = data.getOptions();
        for (QuestionsOption option : options) {
            option.setQid(questions.getId());
            questionsOptionMapper.insert(option);
        }
    }

    @Override
    public IPage<Questions> findList(int page, int pagesize, Integer id) {
        QueryWrapper<Questions> wrapper = new QueryWrapper<>();
        //查询条件
        wrapper.eq("cid", id);
        //返回分页结果
        return questionsMapper.selectPage(new Page<>(page,pagesize),wrapper);
    }

}
